package com.yc.jpyy.vehicle.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.utils.manydatasource.HandlerDataSource;
import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.utils.Tools;
import com.yc.jpyy.vehicle.dao.VehicleDao;
import com.yc.jpyy.vehicle.service.VehicleService;


@Controller
@RequestMapping(value = "/ticket")
@Scope("prototype")
public class TicketController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(TicketController.class);

	@Autowired
	private VehicleService vehicleService;

	@Autowired
	private VehicleDao vehicleDao;
	
	
	/**
	 * 检查票号
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/checkTicket", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String checkTicket(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			//HandlerDataSource.setDataSource("bizDataSource");
			String queryParameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(queryParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = vehicleService.checkTicket(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("checkTicket" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "检查失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 检查票号(循环起始号、结束号进行查询)
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/checkTickets", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String checkTickets(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			String queryParameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(queryParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = vehicleService.checkTickets(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("checkTickets" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "检查失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 插入票数据
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addTicket", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String addTicket(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			//HandlerDataSource.setDataSource("bizDataSource");
			String ticketInfo = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(ticketInfo);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = vehicleService.addTicket(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("addTicket" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "添加失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 查询票
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/queryTiket", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String queryTiket(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			//HandlerDataSource.setDataSource("bizDataSource");
			String queryParameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(queryParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = vehicleService.queryTiket(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("queryTiket" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	
	/**
	 * 获取票证号
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getTicketNum", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String getTicketNum(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			//HandlerDataSource.setDataSource("bizDataSource");
			String queryParameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(queryParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = vehicleService.getTicketNum(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("getTicketNum" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "检查失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 更新明细表状态
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateTicketDetailStatus", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateTicketDetailStatus(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			//HandlerDataSource.setDataSource("bizDataSource");
			String updateParameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(updateParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = vehicleService.updateTicketDetailStatus(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("updateTicketDetailStatus" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "更新失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 更新主表票证状态
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/updateTikeStatus", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String updateTikeStatus(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			String updateParameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(updateParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = vehicleService.updateTikeStatus(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("updateTikeStatus" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "更新失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	/**
	 * 根据ID查询票详情
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getTikeById", method = { RequestMethod.GET,
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	public @ResponseBody String getTikeById(HttpServletRequest request) throws UnsupportedEncodingException {

		Map<String,Object> resultMap = new HashMap<>();
		request.setCharacterEncoding("UTF-8");
		try {
			String queryParameter = request.getParameter("info");
			Map<String,Object> paramsMap = Tools.JsonToMap(queryParameter);
			String dbcode = paramsMap.get("dbcode").toString();
			HandlerDataSource.setDataSource(dbcode);
			resultMap = vehicleService.getTikeById(paramsMap);
			resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
		} catch (Exception e) {
			logger.info("getTikeById" + e.toString());
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "查询失败");
		} finally {
			HandlerDataSource.clear();
			return JSON.toJSONString(resultMap);
		}
	}
	
	    //修改
		@SuppressWarnings("finally")
		@RequestMapping(value = "/updateTicket", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String updateTicket(HttpServletRequest request) throws UnsupportedEncodingException {
			
			RestResponse response = new RestResponse(Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			
			try{
				String info = request.getParameter("info");
				System.out.println(info);
				Map<String,Object> paramsMap = Tools.JsonToMap(info);
				String dbcode = paramsMap.get("dbcode").toString();
				//设置数据源
			    HandlerDataSource.setDataSource(dbcode);
			    vehicleService.updateTicketServer(paramsMap);
				response.setReturnMsg(1, "操作成功");
				return response.toJSON();
				
			} catch (Exception e) {
				logger.info("修改异常" + e.toString());
				// 异常信息
				response.setReturnCode(0);
				response.setReturnMsg("操作失败");
				e.printStackTrace();
			}
			
			finally {
				HandlerDataSource.clear();
				return response.toJSON();
			}
			
		}
		
		/**
		 * 根据ID获取明细表数据
		 * 
		 * @param request
		 * @return
		 * @throws UnsupportedEncodingException
		 */
		@SuppressWarnings("finally")
		@RequestMapping(value = "/getTikeDetailInfo", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String getTikeDetailInfo(HttpServletRequest request) throws UnsupportedEncodingException {

			Map<String,Object> resultMap = new HashMap<>();
			request.setCharacterEncoding("UTF-8");
			try {
				String updateParameter = request.getParameter("info");
				Map<String,Object> paramsMap = Tools.JsonToMap(updateParameter);
				String dbcode = paramsMap.get("dbcode").toString();
				HandlerDataSource.setDataSource(dbcode);
				resultMap = vehicleService.getTikeDetailInfoByTicketId(paramsMap);
				resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			} catch (Exception e) {
				logger.info("getTikeDetailInfo" + e.toString());
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "获取失败");
			} finally {
				HandlerDataSource.clear();
				return JSON.toJSONString(resultMap);
			}
		}
		
		/**
		 * 从明细表取数据  
		 * 
		 * @param request
		 * @return
		 * @throws UnsupportedEncodingException
		 */
		@SuppressWarnings("finally")
		@RequestMapping(value = "/getInfofromTikeDetail", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String getInfofromTikeDetail(HttpServletRequest request) throws UnsupportedEncodingException {

			Map<String,Object> resultMap = new HashMap<>();
			request.setCharacterEncoding("UTF-8");
			try {
				String updateParameter = request.getParameter("info");
				Map<String,Object> paramsMap = Tools.JsonToMap(updateParameter);
				String dbcode = paramsMap.get("dbcode").toString();
				HandlerDataSource.setDataSource(dbcode);
				resultMap = vehicleService.getInfofromTikeDetail(paramsMap);
				resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			} catch (Exception e) {
				logger.info("getInfofromTikeDetail" + e.toString());
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "获取失败");
			} finally {
				HandlerDataSource.clear();
				return JSON.toJSONString(resultMap);
			}
		}
		
		
		/**
		 * 检查票证号是否存在
		 * 
		 * @param request
		 * @return
		 * @throws UnsupportedEncodingException
		 */
		@SuppressWarnings("finally")
		@RequestMapping(value = "/checkNumIsExist", method = { RequestMethod.GET,
				RequestMethod.POST }, produces = "application/json; charset=utf-8")
		public @ResponseBody String checkNumIsExist(HttpServletRequest request) throws UnsupportedEncodingException {

			Map<String,Object> resultMap = new HashMap<>();
			request.setCharacterEncoding("UTF-8");
			try {
				//HandlerDataSource.setDataSource("bizDataSource");
				String queryParameter = request.getParameter("info");
				Map<String,Object> paramsMap = Tools.JsonToMap(queryParameter);
				String dbcode = paramsMap.get("dbcode").toString();
				HandlerDataSource.setDataSource(dbcode);
				resultMap = vehicleService.checkNumIsExist(paramsMap);
				resultMap.put("pubKey",Optional.ofNullable(request.getAttribute("pubKey")).orElse(""));
			} catch (Exception e) {
				logger.info("checkNumIsExist" + e.toString());
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "检查失败");
			} finally {
				HandlerDataSource.clear();
				return JSON.toJSONString(resultMap);
			}
		}
}
